package com.eastcom.providor.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.eastcom.bm.atom.Atom_PublicNe;
import com.eastcom.bm.atom.DataAbnormal;
import com.eastcom.bm.atom.RawData;
import com.eastcom.bm.dataprocess.processor.ModelInfo;
import com.eastcom.client.MapEntryNode;
import com.eastcom.client.ReturnNode;
import com.eastcom.common.db.DataSourceFactory;
import com.eastcom.common.db.JDBCTemplate;
import com.eastcom.common.util.CharliDate;
import com.eastcom.providor.dao.IPublicNeDao;
import com.eastcom.providor.service.BaseService;
import com.eastcom.providor.service.IPublicNeService;
import com.eastcom.providor.service.ServiceFactory;
import com.eastcom.providor.service.extview.ConfigManagerUtil;
import com.eastcom.providor.service.extview.PublicNeUtil;

public class PublicNeService extends BaseService implements IPublicNeService {

	private IPublicNeDao publicNeDao;
	
	
	public void setPublicNeDao(IPublicNeDao publicNeDao) {
		this.publicNeDao = publicNeDao;
	}
	
	public List<Atom_PublicNe> getAllPublicNe() {
		// TODO Auto-generated method stub
		try{
			//IPublicNeDao publicNeDao=DAOFactory.getInstance().getPublicNeDao();
			return publicNeDao.getAllPublicNe();
		}
		catch(Exception e){
			log.error("IPublicNeService getAllPublicNe error", e);
		}
		/*
		finally{
			JDBCTemplate.doSessionFinally();
		}
		*/
		
		return null;
	}
	
	
	
	
	public List<MapEntryNode> getPublicNeByDatasetID(String dasasetID){
		try{
		List<Atom_PublicNe> list=publicNeDao.getPublicNeByDataSetId(dasasetID);
		
		return ConfigManagerUtil.createConfigManagerPublicNeSelect(list);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("publicNeDao getPublicNeByDataSetId  error", e);
		}
		return null;
	}

	public List<MapEntryNode> getAllPublicNeByDatasetID(String dasasetID){
		try{
		List<Atom_PublicNe> list=publicNeDao.getAllPublicNeByDataSetId(dasasetID);
		
		return ConfigManagerUtil.createConfigManagerPublicNeSelect(list);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("publicNeDao getAllPublicNeByDataSetId  error", e);
		}
		return null;
	}
	
	public List<MapEntryNode> getPublicNeByNeType(String neType){
		try{
			List<Atom_PublicNe> list=publicNeDao.getPublicNeByNeType(neType);
			return ConfigManagerUtil.createConfigManagerPublicNeSelect(list);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("publicNeDao getPublicNeByNeType  error", e);
		}
		return null;
	}
	
	
	public Map<String,Object> getPublicNeByID(String pubNeID){
		try {

			return PublicNeUtil.createPublicNeBean(publicNeDao.getPublicNeByID(pubNeID));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("publicNeDao   getPublicNeByID",e);
		}
		return null;
	}
	
	public ReturnNode addPublicNe(Map<String,String> args){
		ReturnNode ret=new ReturnNode();
		ret.setSuccess(false);
		ret.setOperatorArg(args);
		if(args==null||args.get("id")==null){
			ret.setReturnStr("the publicNe Id is null");
			return ret;
		}
		try {
		if(publicNeDao.getPublicNeByID(args.get("id"))!=null){
			ret.setReturnStr("the public Id is Exist");
			return ret;
		}
			
		Atom_PublicNe at=PublicNeUtil.getPublicNeByArgs(args);
			publicNeDao.savePublicNe(at);
			ret.setSuccess(true);
			return ret;
		} catch (Exception e) {
			ret.setReturnStr(e.getMessage());
			e.printStackTrace();
			log.error("publicNeDao addPublicNe ",e);
		}
		return ret;
	}
	
	public ReturnNode editPublicNe(Map args){
		ReturnNode ret=new ReturnNode();
		ret.setSuccess(false);
		ret.setOperatorArg(args);
		if(args==null||args.get("id")==null){
			ret.setReturnStr("the publicNe Id is null");
			return ret;
		}
		Atom_PublicNe at=PublicNeUtil.getPublicNeByArgs(args);
		try {
			publicNeDao.updatePublicNe(at);
			ret.setSuccess(true);
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("publicNeDao editPublicNe",e);
		}
		return null;
	}
	
	public ReturnNode deletePublicNe(String pubNeID){
		ReturnNode ret=new ReturnNode();
		ret.setSuccess(false);
		ret.setOperatorArg(pubNeID);
		try {
			publicNeDao.deletePublicNe(pubNeID);
			ret.setSuccess(true);
			return ret;
		} catch (Exception e) {
			ret.setReturnStr(e.getMessage());
			e.printStackTrace();
			log.error("publicNeDao deletePublicNe",e);
		}
		return ret;
	}
	
	
	public static void main(String[] args){
		try {
			System.out.println(CharliDate.getDayOfWeekOfTimeStamp("2011-3-1 13:00:00"));
		} 
		catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		List<DataAbnormal> unNormalRawDataLst=new ArrayList<DataAbnormal>();
		DataAbnormal ab=new DataAbnormal();
		ab.setAbnormalDesc("AA");
		ab.setAbnormalType("1");
		ab.setDatasetId("1111");
		ab.setDataTime(new Date());
		ab.setNeId("222");
		ab.setState("1");
		ab.setTimeStamp(new Date());
		unNormalRawDataLst.add(ab);
		
		ab=new DataAbnormal();
		ab.setAbnormalDesc("AAA");
		ab.setAbnormalType("1");
		ab.setDatasetId("2222");
		ab.setDataTime(new Date());
		ab.setNeId("111");
		ab.setState("1");
		ab.setTimeStamp(new Date());
		unNormalRawDataLst.add(ab);
		
		List<RawData> rawDataLst=new ArrayList<RawData>();
		RawData raw=new RawData();
		raw.setKpiId("111");
		raw.setNeId("111");
		raw.setDatasetId("111");
		raw.setIsNormal("1");
		raw.setCollectorDate((long)12348);
		raw.setTimeStamp(new Date());
		raw.setValidationType("1");
		raw.setValue(98.8);
		rawDataLst.add(raw);
		
		raw=new RawData();
		raw.setKpiId("222");
		raw.setNeId("222");
		raw.setDatasetId("222");
		raw.setIsNormal("1");
		raw.setCollectorDate((long)12348);
		raw.setTimeStamp(new Date());
		raw.setValidationType("1");
		raw.setValue(98.8);
		rawDataLst.add(raw);
		
		try{
			DataSourceFactory.initial();
			JDBCTemplate.jdbcBatchInsertObjects(rawDataLst, ModelInfo.rawDataOrMapping, true, true, JDBCTemplate.REQUIRES_NEW_COMMIT);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		JDBCTemplate.doSessionFinally();
		
		IPublicNeService service=ServiceFactory.getInstance().getPublicNeService();
		List<Atom_PublicNe> results=service.getAllPublicNe();
		results.size();
		DataSourceFactory.getInstance().destory();
	}
	
}
